package com.tjbank.cssys.service.config.shiro.stateless.token;

import com.tjbank.cssys.service.util.IYCPEncryptor;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.errors.EncryptionException;

/**
 * token生成器
 */
public class TokenGenerator {
    /**
     * 生成用户登陆token
     *
     * @param loginCode
     * @param ts
     * @param seed
     * @return
     * @throws EncryptionException
     */
//    public static String genToken(String loginCode, long ts, String seed) throws EncryptionException {
//        return ESAPI.encryptor().hash(loginCode + ts, seed);
//    }
    public static String genToken(String loginCode, long ts, String seed, long currentDateTime) throws Exception {
        return IYCPEncryptor.encrypt(loginCode + "_" + ts + seed + currentDateTime);
    }

    public static TokenInfo genToken(TokenParameter tokenParameter) throws Exception {
        TokenInfo ti = new TokenInfo();
        ti.setToken(genToken(tokenParameter.getLoginCode(), tokenParameter.getLoginTs(), getSeed(),System.currentTimeMillis()));
        return ti;
    }

    /**
     * 取得系统的加密seed
     *
     * @return
     * @throws EncryptionException
     */
    public static String getSeed() throws EncryptionException {
        // FIXME 随便写的
        return ESAPI.encryptor().hash(new String(ESAPI.securityConfiguration().getByteArrayProp("Encryptor.MasterKey")),
                new String(ESAPI.securityConfiguration().getByteArrayProp("Encryptor.MasterSalt")));
    }
}
